#pragma once

#include "Variable.h"

#include "Action.h"
#include "Activity.h"
#include "StructuredActivityNode.h"

namespace uml {

void Variable::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    ConnectableElement::fromJson(json, errorList);
    MultiplicityElement::fromJson(json, errorList);
    activityScope_ = JsonSerializable::create<Activity>(json.value("activityScope"), errorList);

    scope_ = JsonSerializable::create<StructuredActivityNode>(json.value("scope"), errorList);

}

void Variable::toJson(QJsonObject& json) {
    ConnectableElement::toJson(json);
    MultiplicityElement::toJson(json);
    if (activityScope_) {
        QJsonObject activityScope_json;
        activityScope_->toJson(activityScope_json);
        json.insert("activityScope", activityScope_json);
    }
    if (scope_) {
        QJsonObject scope_json;
        scope_->toJson(scope_json);
        json.insert("scope", scope_json);
    }
}

/// <summary>
/// isAccessibleBy
    /// A Variable is accessible by Actions within its scope (the Activity or
    /// StructuredActivityNode that owns it).
/// </summary>
    /// <code>
    /// result = (if scope<>null then scope.allOwnedNodes()->includes(a)
    /// else a.containingActivity()=activityScope
    /// endif)
    /// </code>
bool Variable::isAccessibleBy(shared_ptr<Action> a) const
{
    return false;
}

/// <summary>
/// activityScope getter
/// An Activity that owns the Variable.
/// </summary>
shared_ptr<Activity> Variable::activityScope() const {
    return activityScope_;
}

/// <summary>
/// activityScope setter
/// An Activity that owns the Variable.
/// </summary>
void Variable::setActivityScope(shared_ptr<Activity> value) {
    activityScope_ = value;
}

/// <summary>
/// scope getter
/// A StructuredActivityNode that owns the Variable.
/// </summary>
shared_ptr<StructuredActivityNode> Variable::scope() const {
    return scope_;
}

/// <summary>
/// scope setter
/// A StructuredActivityNode that owns the Variable.
/// </summary>
void Variable::setScope(shared_ptr<StructuredActivityNode> value) {
    scope_ = value;
}

} // namespace uml
